/**
 * 配置
 * */
package org.zmhhxl.sample3.oauth2.a.authentications.strategy.impl;

import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Component;
import org.zmhhxl.sample3.oauth2.a.authentications.model.CustomOAuth2User;
import org.zmhhxl.sample3.oauth2.a.authentications.strategy.Oauth2UserConverterStrategy;

import java.util.Map;

import static org.zmhhxl.sample3.oauth2.a.authentications.constant.AuthenticationConstant.THIRD_LOGIN_GITEE;

/**
 * 转换通过码云登录的用户信息
 *
 * @author zmhhxl
 */
@Component(THIRD_LOGIN_GITEE)
public class GiteeUserConverter implements Oauth2UserConverterStrategy {

   @Override
   public CustomOAuth2User convert(OAuth2User oAuth2User) {
      // 获取三方用户信息
      Map<String, Object> attributes = oAuth2User.getAttributes();
      // 转换至 BasicOAuth2User
      CustomOAuth2User basicOauth2User = new CustomOAuth2User(oAuth2User, null);
      basicOauth2User.setUniqueId(String.valueOf(attributes.get("id")));
      basicOauth2User.setThirdUsername(oAuth2User.getName());
      basicOauth2User.setType(THIRD_LOGIN_GITEE);
      //basicOauth2User.setBlog(String.valueOf(attributes.get("blog")));
      // 设置基础用户信息
      basicOauth2User.setName(oAuth2User.getName());
      // 设置头像
      basicOauth2User.setAvatarUrl(String.valueOf(attributes.get("avatar_url")));
      return basicOauth2User;
   }
}
